package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfonRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("users")
public class UserController {
    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.appkey}")
    private String appkey;

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfonRedisService userInfonRedisService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.getOne(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        boolean ar = userInfoService.checkPhone(phone);

        return ar;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone, String smsUrl, String appkey) {
        smsUrl = this.smsUrl;
        appkey = this.appkey;
        userInfoService.sendVerifyCode(phone, smsUrl, appkey);

        return JsonResult.success();
    }

    //用户注册
    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);

        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username, String password) {
        //登录成功后返会user
        UserInfo user = userInfoService.login(username, password);
        //创建token并把对象存入redis
        String token = userInfonRedisService.setToken(user);
        //页面要的数据是  user token故用map集合把它传过去
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", user);
        return JsonResult.success(map);
    }

    @GetMapping("/currentUser")
    @RequireLogin
    public Object currentUser(HttpServletRequest request) {

        String token = request.getHeader("token");
        UserInfo user = userInfonRedisService.getUserByTohen(token);
        System.out.println(user);
        return JsonResult.success(user);
    }
   //根据用户id查 攻略id 集合
    @GetMapping("/strategies/favor")
    public Object getstrategiesFavor(String  userId) {

        return JsonResult.success(strategyStatisVORedisService.getUserStrategiesFavor(userId));
    }


}
